package com.thomas.controller.admin;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.thomas.entity.Blog;
import com.thomas.entity.BlogType;
import com.thomas.entity.Blogger;
import com.thomas.entity.Link;
import com.thomas.service.BlogService;
import com.thomas.service.BlogTypeService;
import com.thomas.service.BloggerService;
import com.thomas.service.LinkService;
import com.thomas.util.ResponseUtil;

/** 
 * @ClassName: SystemAdminController 
 * @Description: 管理员系统Controller 
 * @author thomas_liu  
 * @date 2017年9月29日 上午11:33:55 
 */
@Controller
@RequestMapping("/admin/system")
public class SystemAdminController {
	
	@Autowired
	private BloggerService bloggerService;
	
	@Autowired
	private LinkService linkService;
	
	@Autowired
	private BlogTypeService blogTypeService;
	
	@Autowired
	private BlogService blogService;
	
	/**
	 *刷新系统缓存
	 *@param request
	 *@throws Exception 
	 */
	@RequestMapping("/refreshSystem")
	public String refreshSystem(HttpServletResponse response,HttpServletRequest request) throws Exception{
		ServletContext application = RequestContextUtils.getWebApplicationContext(request).getServletContext();
		
		Blogger blogger = bloggerService.find(1);//获取博主信息
		blogger.setPassword(null);
		application.setAttribute("blogger", blogger);
		
		List<Link> linkList = linkService.list(null);//查询所有的友情链接信息
		application.setAttribute("linkList", linkList);
		
		List<BlogType> blogTypeList = blogTypeService.countList();//查询博客类别以及博客的数量
		application.setAttribute("blogTypeList", blogTypeList);
		
		List<Blog> blogCountList = blogService.countList();//根据日期分组查询博客
		application.setAttribute("blogCountList", blogCountList);
		
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}

}
